import com.documentum.com.*;
import com.documentum.fc.common.*;
import com.documentum.fc.client.*;
import java.util.*;

public class CabinetLister {
    private String username = null;
    private String password = null;
    private String docbase = null;
    private final static String DQL = "select object_name from dm_cabinet order by object_name";

    public CabinetLister(String username, String password, String docbase) {
        this.username = username;
        this.password = password;
        this.docbase = docbase;
    }
    
    public List<String> getCabinetNames() throws DfException {
        List<String> cabinetNames = new ArrayList<String>();
        IDfClientX clientx = new DfClientX();
        IDfClient client = null;
        IDfSessionManager manager = null;
        IDfSession session = null;
        IDfLoginInfo loginInfo = null;
        IDfCollection results = null;
        IDfQuery query = null;

        try {
            loginInfo = clientx.getLoginInfo();
            loginInfo.setUser(username);
            loginInfo.setPassword(password);

            client = clientx.getLocalClient();
            manager = client.newSessionManager();
            manager.setIdentity(docbase, loginInfo);
            session = manager.getSession(docbase);
            System.out.println("Connected to docbase " + session.getDocbaseName() + " OK");
            
            query = clientx.getQuery();
            query.setDQL(DQL);
            results = query.execute(session, IDfQuery.DF_READ_QUERY);
            if(results != null) {
                while(results.next()) {
                    cabinetNames.add(results.getString("object_name"));
                }
            }
        } finally {
            if(manager != null && session != null) {
                if(results != null) results.close();
                manager.release(session);
                System.out.println("Disconnected OK");
            }
        }
        
        return cabinetNames;
    }
    
    public static void main(String[] args) {
        final String separator = "--------------------";
        if(args.length == 3) {
            try {
                System.out.println(separator);
                List<String> cabinetNames = new CabinetLister(args[0], args[1], args[2]).getCabinetNames();
                System.out.println(separator);
                for(String cabinetName : cabinetNames) {
                    System.out.println("Found cabinet = " + cabinetName);
                }
            } catch(DfException e) {
                System.out.println("Error detected\n" + e.getMessage());
            }

        } else {
            System.out.println("Usage: java CabinetLister username password docbase");
        }
    }
}